package CreationPattern.Prototype;

/**
 * @Classname : SheepClient
 * @Description : 测试 new 和 clone 的创建对象效率
 * @Author : chentianyu
 * @Date 2023/1/23 23:32
 */


public class SheepClient {

    public static void main(String[] args) {
        testNew(10000);
        testClone(10000);
    }

    /**
     * new 创建对象
     *
     * @param num
     */
    public static void testNew(int num){
        long start = System.currentTimeMillis();
        for(int i=0; i<num; i++){
            DeepCopySheep sheep = new DeepCopySheep(i + "");
        }
        long end = System.currentTimeMillis();
        System.out.println("new 总耗时：" + (end-start) + "ms");
    }

    /**
     * clone 的创建对象
     *
     * @param num
     */
    public static void testClone(int num) {
        DeepCopySheep sheep = new DeepCopySheep( "多莉");
        long start = System.currentTimeMillis();
        for(int i=0; i<num; i++){
            DeepCopySheep copySheep = sheep.clone();
        }
        long end = System.currentTimeMillis();
        System.out.println("clone 总耗时：" + (end-start) + "ms");
    }
}

/** 20230123
 * new 总耗时：44ms
 * clone 总耗时：2ms
 */
